import { Command } from "commander";
import { TestFactory } from "./src/test-factory";

export type StressArgs = {
    node: string;
    senderPrivateKey: string;
    receiverAddress: string;
    accounts: number;
    transactions: number;
};

const parse = (args: any): StressArgs | undefined => {
    const validNumber = (n: any): boolean => !isNaN(Number(n)) && Number(n) >= 0;
    if (args.accounts && validNumber(args.accounts) && args.transactions && validNumber(args.transactions)) {
        return {
            node: args.node || "https://rpc-evm-sidechain.xrpl.org",
            senderPrivateKey: args.senderPrivateKey || "f5ac42fd86cdb55f0ad85f201b5e929188213316787b171bb4626c30dce03a79",
            receiverAddress: args.receiverAddress || "0x68f727c3cd7aeB5a04acB864B770f5aa193676Bd",
            accounts: args.accounts,
            transactions: args.transactions,
        };
    }
    return undefined;
};

const program = new Command("stress")
    .option("-n, --node <value>", "The url of the node to run the script on")
    .option("-s, --sender-private-key <value>", "The private key with funds used for creating transactions")
    .option("-r, --receiver-address <value>", "The receiver address of the funds")
    .option("-a, --accounts <number>", "Amount of accounts to be created")
    .option("-t, --transactions <number>", "Amount of transactions per account")
    .action(async (args) => {
        const parsedArgs = parse(args);
        if (parsedArgs) {
            const testFactory = new TestFactory(parsedArgs);
            try {
                await testFactory.start();
            } catch (error) {
                console.log(error);
                process.exit(1);
            }
            process.exit(0);
        } else {
            program.help();
            process.exit(1);
        }
    });
export default program;
